<?php
/**
 * svn info svn://ip/path --username user --password pass
 * @author Alan.H
 */
require_once dirname(__FILE__) . "/../Command.php";

/**
 * Class Info
 * @method setUrl(string $url);
 * @method getUrl();
 * @method setRelativeUrl(string $url);
 * @method getRelativeUrl();
 * @method setRoot(string $root);
 * @method getRoot();
 * @method setUuid(string $uuid);
 * @method getUuid();
 * @method setRevision(string $revision);
 * @method getRevision();
 * @method setAuthor(string $author);
 * @method getAuthor();
 * @method setDate(string $date);
 * @method getDate();
 */
class Info extends Command {
    /**
     * @return $this|mixed
     */
    public function execCmd() {
        $cmd = [
            'svn',
            'info',
            $this->log->getUrl(),
            '--username',
            $this->log->getUsername(),
            '--password',
            $this->log->getPassword(),
        ];

        $name  = md5(join('', $cmd));
        $file  = $this->log->getRuntime() . '/' . $name . '.xml';
        $cmd[] = '--xml';
        $cmd[] = '>';
        $cmd[] = $file;

        if ( ! file_exists($file)) {
            exec(join(' ', $cmd));
        }

        $xml = simplexml_load_file($file);
        $this->parseXml($xml);

        $this->file = $file;
        return $this;
    }

    /**
     * @param $xml
     */
    public function parseXml($xml) {
        $this->setUrl((string)$xml->entry->url);
        $this->setRelativeUrl(trim((string)$xml->entry->{"relative-url"}, '^'));

        $repository = $xml->entry->repository;
        $this->setRoot((string)$repository->root);
        $this->setUuid((string)$repository->uuid);

        $commit = $xml->entry->commit;
        $this->setAuthor((string)$commit->author);
        $this->setDate($this->formatDate((string)$commit->date));
        $this->setRevision((string)$commit['revision']);
    }
}